package com.cal.utms.action.dayEnd;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.cal.utms.dao.dayEnd.DayEndDao;
import com.cal.utms.dao.maintenance.NewFundDao;
import com.cal.utms.dto.dayEnd.DayEndDto;
import com.cal.utms.entity.Fund;
import com.cal.utms.model.dayEnd.DayEndModel;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class DayEndAction extends ActionSupport implements
ModelDriven<DayEndDto> {
	
	private static final long serialVersionUID = 1L;
	private DayEndDto dayEndDto = new DayEndDto();

	@Override
	public DayEndDto getModel() {
		return this.dayEndDto;
	}
	
	public String queryPendingMoneyTransactions(){
		int fundId = this.validateFund(dayEndDto);
		Date valueDate = this.validateValueDate(dayEndDto);
		if(dayEndDto.isSuccess()){
			DayEndDao dayEndDao = new DayEndDao();
			dayEndDao.getRemainingMoneyTransactions(fundId, dayEndDto.getQueryTwo(), valueDate, dayEndDto);
		}
		return Action.SUCCESS;
	}
	
	public String queryPendingReceiveInstruments(){
		//http://localhost:8080/UnitTrust/pendingIssueIns.action?queryOne=5&queryTwo=CUS&valueDate=09/22/2014
		
		int fundId = this.validateFund(dayEndDto);
		Date valueDate = this.validateValueDate(dayEndDto);
		if(dayEndDto.isSuccess()){
			DayEndDao dayEndDao = new DayEndDao();
			dayEndDao.getPendingInstrumentReceive(fundId, valueDate, dayEndDto);
		}
		return Action.SUCCESS;
	}
	
	public String queryPendingIssueInstruments(){
		//http://localhost:8080/UnitTrust/pendingIssueIns.action?queryOne=5&queryTwo=CUS&valueDate=09/22/2014

		int fundId = this.validateFund(dayEndDto);
		Date valueDate = this.validateValueDate(dayEndDto);
		if(dayEndDto.isSuccess()){
			DayEndDao dayEndDao = new DayEndDao();
			dayEndDao.getPendingInstrumentIssue(fundId, valueDate, dayEndDto);
		}
		return Action.SUCCESS;
	}
	
	
	
	public String getPendingInvestments(){
		//http://localhost:8080/UnitTrust/pendingInvests.action?queryOne=5&queryTwo=CUS&valueDate=09/22/2014

		int fundId = this.validateFund(dayEndDto);
		Date valueDate = this.validateValueDate(dayEndDto);
		if(dayEndDto.isSuccess()){
			DayEndDao dayEndDao = new DayEndDao();
			dayEndDao.queryPendingInvestments(fundId, valueDate, dayEndDto);
		}
		return Action.SUCCESS;
	}
	
	public String getPendingRedemptions(){
		//http://localhost:8080/UnitTrust/pendingRedemps.action?queryOne=24&queryTwo=CUS&valueDate=09/22/2014

		int fundId = this.validateFund(dayEndDto);
		Date valueDate = this.validateValueDate(dayEndDto);
		if(dayEndDto.isSuccess()){
			DayEndDao dayEndDao = new DayEndDao();
			dayEndDao.queryPendingRedemptions(fundId, valueDate, dayEndDto);
		}		
		return Action.SUCCESS;
	}
	
	public String getPendingDeals(){
		//http://localhost:8080/UnitTrust/pendingDeals.action?queryOne=24&queryTwo=CUS&valueDate=09/22/2014

		int fundId = this.validateFund(dayEndDto);
		Date valueDate = this.validateValueDate(dayEndDto);
		if(dayEndDto.isSuccess()){
			DayEndDao dayEndDao = new DayEndDao();
			dayEndDao.queryPendingDeals(fundId, valueDate, dayEndDto);
		}		
		return Action.SUCCESS;
	}
	
	public String getPendingPayments(){
		//http://localhost:8080/UnitTrust/pendingPayments.action?queryOne=24&queryTwo=CUS&valueDate=09/22/2014

		int fundId = this.validateFund(dayEndDto);
		Date valueDate = this.validateValueDate(dayEndDto);
		if(dayEndDto.isSuccess()){
			DayEndDao dayEndDao = new DayEndDao();
			dayEndDao.queryPendingPayments(fundId, valueDate, dayEndDto);
		}		
		return Action.SUCCESS;
	}
	
	
	public String getPendingReceipts(){
		//http://localhost:8080/UnitTrust/pendingReceipts.action?queryOne=24&queryTwo=CUS&valueDate=09/22/2014

		int fundId = this.validateFund(dayEndDto);
		Date valueDate = this.validateValueDate(dayEndDto);
		if(dayEndDto.isSuccess()){
			DayEndDao dayEndDao = new DayEndDao();
			dayEndDao.queryPendingReceipts(fundId, valueDate, dayEndDto);
		}		
		return Action.SUCCESS;
	}
	
	public String getPendingFloatingRates(){
		//http://localhost:8080/UnitTrust/pendingFloatingRates.action?queryOne=24&queryTwo=CUS&valueDate=12/30/2014

		int fundId = this.validateFund(dayEndDto);
		Date valueDate = this.validateValueDate(dayEndDto);
		if(dayEndDto.isSuccess()){
			DayEndDao dayEndDao = new DayEndDao();
			dayEndDao.queryPendingFloatingRates(fundId, valueDate, dayEndDto);
		}		
		return Action.SUCCESS;
	}
	
	public String getPendingCollaterals(){
		//http://localhost:8080/UnitTrust/pendingCollaterals.action?queryOne=24&queryTwo=CUS&valueDate=12/30/2014

		int fundId = this.validateFund(dayEndDto);
		Date valueDate = this.validateValueDate(dayEndDto);
		if(dayEndDto.getQueryTwo()==null){
			dayEndDto.setStatus("Deal Type is empty");
			dayEndDto.setSuccess(false);
		}
		if(dayEndDto.isSuccess()){
			DayEndDao dayEndDao = new DayEndDao();
			dayEndDao.queryPendingCollaterals(fundId, valueDate, dayEndDto);
		}		
		return Action.SUCCESS;
	}
	
	public String startEod(){
		//http://localhost:8080/UnitTrust/runEod.action?queryOne=24&queryTwo=CUS&valueDate=12/30/2014

		int fundId = this.validateFund(dayEndDto);
		Date valueDate = this.validateValueDate(dayEndDto);
		
		Fund fund = new NewFundDao().getById(fundId);
		if(fund==null){
			dayEndDto.setStatus("Cannot Find the Fund : "+fundId);
			dayEndDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(dayEndDto.isSuccess()){
			DayEndModel dayEndModel = new DayEndModel();
			dayEndModel.runDayEndProcess(fund, valueDate, dayEndDto);
		}		
		return Action.SUCCESS;
	}
	
	public int validateFund(DayEndDto dayEndDto){
		int fundId = 0;
		dayEndDto.setSuccess(true);
		try{
			fundId = Integer.parseInt(dayEndDto.getQueryOne());
		}catch(NumberFormatException nfe){
			dayEndDto.setSuccess(false);
			dayEndDto.setStatus("Invalid Fund ID "+ dayEndDto.getQueryOne());
			return fundId;
		}
		NewFundDao nfd = new NewFundDao();
		Fund fund = nfd.getById(fundId);
		if(fund==null){
			dayEndDto.setSuccess(false);
			dayEndDto.setStatus("Cannot Find Fund : Id "+ dayEndDto.getQueryOne());
			return fundId;
		}
		return fundId;
	}
	
	public Date validateValueDate(DayEndDto dayEndDto){
		Date valueDate = null;
		if(dayEndDto.getValueDate()!=null){
			DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
			try{
				valueDate = df.parse(dayEndDto.getValueDate());
			}catch(ParseException pe){
				dayEndDto.setSuccess(false);
				dayEndDto.setStatus("Value Date Parse Error!");
				return valueDate;
			}			
		}else{
			dayEndDto.setSuccess(false);
			dayEndDto.setStatus("Empty Value Date!");
			return valueDate;
		}
		return valueDate;
	}
	
	

}
